<?php

require_once '../global.php';
//已读未读
function king_ajax_read(){
	global $king;
	$king->access('soso_edt');
	$kid=kc_get('list',2,1);
	$value=kc_get('value',2,1);
	$king->db->query("update %s_soso set nread=$value where kid=$kid;");
	$value ? $ico='n2':$ico='n1';
	kc_ajax('',kc_icon($ico,($value?$king->lang->get('soso/list/unread'):$king->lang->get('soso/list/read'))),0,"$('#nread_{$kid}').attr('rel','{CMD:\'read\',value:".(1-$value).",ID:\'nread_{$kid}\',list:\'$kid\',IS:2}')");
}

//删除留言
function king_ajax_delete(){
	global $king;
	$king->access('soso_delete');
	$list=kc_getlist();
	$king->db->query("delete from %s_soso where kid in ($list)");
	kc_ajax('OK',"<p class=\"k_ok\">".$king->lang->get('system/ok/delete')."</p>",1);
}

//排序
function king_ajax_updown(){
	global $king;
	$king->access('soso_updown');

	$kid=kc_get('kid',2,1);
	$king->db->updown('%s_soso',$kid);
}
//显示隐藏
function king_ajax_show(){
	global $king;
	$king->access('soso_edt');
	$kid=kc_get('list',2,1);
	$value=kc_get('value',2,1);
	$king->db->query("update %s_soso set nshow=$value where kid=$kid;");
	$value ? $ico='n1':$ico='n2';
	kc_ajax('',
		kc_icon($ico,($value?$king->lang->get('soso/list/unshow'):$king->lang->get('soso/list/show'))),
		0,
		"$('#nshow_{$kid}').attr('rel','{CMD:\'show\',value:".(1-$value).",ID:\'nshow_{$kid}\',list:\'$kid\',IS:2}')");
}

/**
菜单调用
*/
function inc_menu(){
	global $king;
	$left=array(
		''=>array(
			'href'=>'manage.php',
			'ico'=>'p7',
			'title'=>$king->lang->get('system/common/list'),
			),
	);
	if(isset($_GET['kid'])){
		$left['view']=array(
			'href'=>'manage.php?action=view&kid='.$_GET['kid'],
			'ico'=>'p8',
			'title'=>$king->lang->get('system/common/view'),
		);
	}
	return array($left,array());
}



function king_def(){
	global $king;
	$king->access('soso');
	


	require_once 'class_ciji.php';/*必须的*/
	require_once 'Snoopy.class.php';/*必须的*/
	$spider  = new spider();

	$keywords=kc_get('ktitle',0); 
	$page=kc_get('pid','0');
	
	$url = urlencode($keywords);

	$spider->kuiurl = "http://wenwen.soso.com/z/QzoneSearch.e?sp=S".$url."&pg=".$page;
	//$spider->kuiurl = "http://www.baidu.com";
	$spider->getContent();
	
	$spider->kuibody=array('<body','</body>');
	$arraly=array('#<h4><a class="c_tx" href=\"(.*)\=(.*)\"><span class="c_tx">(.*)</span></a></h4>(.*)<div class="bd">(.*)</div>(.*)<div class="ft">(.*)</div>#Us','yes');
	$kuibody=$spider->gettitle($arraly[0],$arraly[1]);//标题
	$arraly2=array('#<td>(.*)class="c_tx">(.*)</a></td>#Us','yes');
	$kuify=$spider->gettitle($arraly2[0],$arraly2[1]);//标题

	$sd[0]=$kuibody[2];
	$sd[1]=$kuibody[3];
	$sd[2]=$kuibody[7];


	foreach($sd[0] as $key => $val){
			$res[$key]['kid']=$sd[0][$key];
			$res[$key]['ktitle']=str_replace(' ','',SpHtml2Text($sd[1][$key]));
			$res[$key]['kcontent']=str_replace(' ','',SpHtml2Text($sd[2][$key]));
			$num=$key;
	}

	


	$_cmd=array(
		'delete'=>$king->lang->get('system/common/del'),
	);
	$manage ="'<a href=\"javascript:;\" class=\"k_ajax\" rel=\"{CMD:\'view\',kid:'+K[0]+'}\">'+$.kc_icon('h6','".$king->lang->get('system/common/brow')."')+'</a>'";



	$_js=array(
		"\$.kc_list(K[0],K[1],'search.php?action=view&kid='+K[0])",
		$manage,

		"K[2]",
	);
	if($num>8){$num=200;}else{$num='';}
	$s=$king->openList($_cmd,'',$_js,$king->db->pagelist('search.php?ktitle='.$keywords.'&pid=PID',$num));




	foreach($sd[0] as $key => $val){
			$res[$key]['kid']=$sd[0][$key];
			$res[$key]['ktitle']=str_replace(' ','',SpHtml2Text($sd[1][$key]));;
			$res[$key]['kcontent']=str_replace(' ','',SpHtml2Text($sd[2][$key]));;
	}



	$s.="function ishow(url,id,is){var I1,ico;is?ico='n1':ico='n2';";
	$s.="I1='<a id=\"nshow_'+id+'\" class=\"k_ajax\" rel=\"{CMD:\'show\',value:'+ (1-is) +',ID:\'nshow_'+id+'\',list:'+id+',IS:2}\" >'+$.kc_icon(ico,(is?'".$king->lang->get('keywords/list/unshow')."':'".$king->lang->get('keywords/list/show')."'))+'</a>';return I1;};";
	$s.="ll('".$king->lang->get('keywords/list/title')."',
	    'manage',


	    '".$king->lang->get('keywords/list/date')."',1);";


	foreach($res as $rs){
		$s.="ll({$rs['kid']},
		    '".addslashes(htmlspecialchars($rs['ktitle']))."',
		    '".$rs['kcontent']."',0);";
	}




	$s.=$king->closeList();

	list($left,$right)=inc_menu();
		$s.='<table class="k_table_form" cellspacing="0"><tr>';
	foreach($kuify[2] as $key => $rs){
		if($key==4){
			$s.='<tr></tr>';
		}
		$s.='<td><a href="search.php?ktitle='.urlencode($rs).'">'.$rs.'</a></td>';
	}
		$s.='<tr></table>';		
	$king->skin->output($king->lang->get('keywords/title/center'),$left, $bs,$s);

	
	}
	
	
function king_ajax_view(){
	global $king;
	$king->access('soso');


	require_once 'class_ciji.php';/*必须的*/
	require_once 'Snoopy.class.php';/*必须的*/
	require_once '../home/library/db.class.php';
	$kid=kc_get('kid');
	$spider  = new spider();
	$spider->kuiurl = "http://wenwen.soso.com/z/QzoneQuestion.e?sp=".$kid;
	$spider->getContent();
	
	$spider->kuibody=array('<body','</body>');
	$arraly=array('#<h1 class="bor3"><span class="icon_done"></span>(.*)</h1>(.*)<div#Us','yes');
	$kuititle=$spider->gettitle($arraly[0],$arraly[1]);//标题
	$arraly2=array('#答：(.*)</div>#Us','1');
	$kuify=$spider->gettitle($arraly2[0],$arraly2[1]);//标题


			if(!empty($kuititle['2']['0'])){
					$kdescription=strip_tags($kuititle['2']['0']);
					$kdescription=preg_replace('/(\&[a-z]{1,6};)|\s/','',$kdescription);
					$_array['kdescription']=kc_substr($kdescription,0,200);
				}else{
					$_array['kdescription']=$kuititle['1']['0'];
				}


				$s.=$king->htmForm('标题 (20-100)字符','<input class="k_in w400" type="text" name="ktitle" value="'.htmlspecialchars($kuititle['1']['0']).'" maxlength="100" />',$_arrays,null,"<tt></tt>");
				//$s.='标题:'.$kuititle['1']['0'].'<br><br><br>';
				//$s.='简介:'.$_array['kdescription'].'<br><br><br>';
				$s.=$king->htmForm('简介','<textarea cols="60" rows="10" style="width:500px;height:100px;" id="kcontent" name="kcontent">'.$_array['kdescription'].'</textarea>',$_arrays,null,"<tt></tt>");
				$s.='内容:'.SpHtml2Text($kuititle['2']['0']).'<br><br><br>';

				
		
				
				foreach($kuify as $key => $rs){	

					$s.='回复:'.SpHtml2Text($rs).'<br><br><br>';
					
				}
			$but=kc_htm_a('提交','{URL:\'search.php\',CMD:\'add\',kid:\''.$kid.'\'}');	
			kc_ajax('查看详情',$s,$but,'',570,450);

	
	
}

function king_ajax_add(){
	global $king;
	$king->access('soso');


	require_once 'class_ciji.php';/*必须的*/
	require_once 'Snoopy.class.php';/*必须的*/
	require_once '../home/library/db.class.php';
	$kid=kc_get('kid');
	$spider  = new spider();
	$spider->kuiurl = "http://wenwen.soso.com/z/QzoneQuestion.e?sp=".$kid;
	$spider->getContent();
	
	$spider->kuibody=array('<body','</body>');
	$arraly=array('#<h1 class="bor3"><span class="icon_done"></span>(.*)</h1>(.*)<div#Us','yes');
	$kuititle=$spider->gettitle($arraly[0],$arraly[1]);//标题
	$arraly2=array('#答：(.*)</div>#Us','1');
	$kuify=$spider->gettitle($arraly2[0],$arraly2[1]);//标题
	$king->load('user');
	
		$userk=$king->user->infoUser(rand(12,840));
	if(empty($userk['userid'])){
		$userk=$king->user->infoUser(11);
	}
	if($res=$king->db->getRows_one("select * from %s_thread where title='".$kuititle['1']['0']."';")){
					kc_ajax('OK',"<p class=\"k_ok\">已经添加过</p>",1);
	}else{

			if(!empty($kuititle['2']['0'])){
					$kdescription=strip_tags($kuititle['2']['0']);
					$kdescription=preg_replace('/(\&[a-z]{1,6};)|\s/','',$kdescription);
					$_array['kdescription']=kc_substr($kdescription,0,200);
				}else{
					$_array['kdescription']=$kuititle['1']['0'];
				}


				$array=array(

					'typeid'=>'1',		
					'userid'=>$userk['userid'],
					'username'=>$userk['username'],		
					'title'=>$_POST[ktitle],
					'ktag'=>keywords_class::k_dangdang_fc($kuititle['1']['0'],'2'),
					'kpath'=>'',
					'kdescription'=>$_POST['kcontent'],
					'lastuserid'=>'11',
					'lastusername'=>$userk['username'],
					'nshow'=>'1',
					'lastdate'=>time(),
				);
				
				$db=new db;
				$threadid=$db->insert('%s_thread',$array);
				
				$array=array(
					'typeid'=>'1',	
					'threadid'=>$threadid,
					'userid'=>$userk['userid'],
					'username'=>$userk['username'],	
					'ip'=>kc_getip(),
					'date'=>time(),
					'floor'=>'1',
					'nshow'=>'1',
					'content'=>SpHtml2Text($kuititle['2']['0']).$kuititle['1']['0'],
				);
				$db->insert('%s_reply',$array);
				
		
				
				foreach($kuify as $key => $rs){	
					$userk=$king->user->infoUser(rand(12,1001));		
					$array=array(
					'typeid'=>'1',	
					'threadid'=>$threadid,
					'userid'=>$userk['userid'],
					'username'=>$userk['username'],	
					'ip'=>kc_getip(),
					'date'=>time(),
					'floor'=>$key+'2',
					'nshow'=>'1',
					'content'=>SpHtml2Text($rs),
					);
					$reply=$key+1;
					$db->insert('%s_reply',$array);							
				}
					$array=array('reply'=>$reply,'hit'=>$reply*rand(2,4));
					$db->update('%s_thread',$array,'threadid='.$threadid);
				
//	$_array['kkeywords']=$kstitle;
//				if(!empty($_array['kkeywords'])){
//						
//							$data_k=explode(',',$_array['kkeywords']);
//						
//							foreach($data_k as $val){
//											
//											$array_k=explode(',',keywords_class::k_dangdang_fc($val));
//				
//													
//													$_array_k=array(
//														'ktitle'=>$val,
//														'ktag'=>keywords_class::k_dangdang_fc($val),
//														'ndate'=>time(),	
//														'kname'=>keywords_class::length($val),
//													);
//				
//												
//													$kid_k=keywords_class::k_ins_key($_array_k);
//				
//				
//													if(!empty($kid_k)){
//														$arrid=keywords_class::k_ins_tag($array_k,$kid_k);
//											
//													}
//										}
//					
//						}	
				
				kc_ajax('OK',"<p class=\"k_ok\">添加成功</p>",1);
	}
	
}



?>